package com.hisense.hiioc.hismartplate.controller;

import com.hisense.hiioc.hismartplate.constants.URLConstant;
import com.hisense.hiioc.hismartplate.utils.UUIDGenerator;
import com.hisense.hiioc.hismartplate.utils.ZxingHandler;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;

/**
 * @Auther:biexiande@hisense.com
 * @date:2018/12/19 14:14
 * @des
 */
@RestController
@RequestMapping(URLConstant.QRcodeMapping.BASE_URL)
public class QRcodeController {

    @GetMapping(value="/encode")
    @ApiOperation(value = "内容编码接口(二维码)")
    public ResponseEntity encode(@ApiParam(value="内容") @RequestParam(value ="content") String content,
                                 @ApiParam(value="宽度") @RequestParam(value ="width",required = false,defaultValue ="200" )  int width,
                                 @ApiParam(value="高度") @RequestParam(value ="height",required = false,defaultValue ="200") int height) {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ZxingHandler.encode2(content, width, height, out);
        HttpHeaders headers = new HttpHeaders();
        String fileName = UUIDGenerator.create32Key()+".png";
        headers.setContentDispositionFormData("attachment",fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        // 201 HttpStatus.CREATED
        return new ResponseEntity<byte[]>(out.toByteArray(),
                headers, HttpStatus.OK);
    }
}